/**
 * @param {Array} list 通过路由列表得到菜单列表
 * @returns {Array}
 */
export const getMenuByRouter = (list) => {
  let res = []
  list.forEach((item) => {
    if (item.meta && !item.meta.hideInMenu) {
      let obj = {
        icon: (item.meta && item.meta.icon) || '',
        name: item.name,
        meta: item.meta
      }
      if (item.children) {
        if (item.meta.hide) {
          let arr = getMenuByRouter(item.children)
          arr.forEach((arrItem) => {
            res.push(arrItem)
          })
        } else {
          obj.children = getMenuByRouter(item.children)
          res.push(obj)
        }
      } else {
        res.push(obj)
      }
    }
  })
  return res
}

const isClass = (o) => {
  if (o === null) return 'Null'
  if (o === undefined) return 'Undefined'
  return Object.prototype.toString.call(o).slice(8, -1)
}

// 深度克隆
export const deepClone = (obj) => {
  if (!obj) { return null }
  let result = isClass(obj)
  let oClass = isClass(obj)
  // 确定result的类型
  if (oClass === 'Object') {
    result = {}
  } else if (oClass === 'Array') {
    result = []
  } else {
    return obj
  }

  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      let copy = obj[key]
      if (isClass(copy) === 'Object') {
        result[key] = deepClone(copy)
      } else if (isClass(copy) === 'Array') {
        result[key] = deepClone(copy)
      } else {
        result[key] = obj[key]
      }
    }
  }
  return result
}
